package com.xuecheng.manage_media_process.mq;

import com.alibaba.fastjson.JSON;
import com.xuecheng.framework.domain.media.MediaFile;
import com.xuecheng.framework.domain.media.MediaFileProcess;
import com.xuecheng.framework.domain.media.MediaFileProcess_m3u8;
import com.xuecheng.framework.utils.HlsVideoUtil;
import com.xuecheng.framework.utils.Mp4VideoUtil;
import com.xuecheng.manage_media_process.dao.MediaFileRepository;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
public class MediaCustomer {
    @Autowired
    MediaFileRepository mediaFileRepository;
    @Value("${xc-service-manage-media.ffmpeg-path}")
    String ffmpeg_path;
    @Value("${xc-service-manage-media.video-location}")
    String video_path;

    @RabbitListener(queues = {"${xc-service-manage-media.mq.queue-media-video-processor}"})
    public void getMessage(String msg) {
//        获取队列消息，获取mediaId
        Map map = JSON.parseObject(msg, Map.class);
        String mediaId = (String) map.get("mediaId");

//        查询数据库获取文件路径
        Optional<MediaFile> optionalMediaFile = mediaFileRepository.findById(mediaId);
        if (!optionalMediaFile.isPresent()) {
            return;
        }
        MediaFile mediaFile = optionalMediaFile.get();
        mediaFile.setFileStatus("303001");
//        调用第三方软件将文件avi转为MP4
//        String ffmpeg_path, String video_path, String mp4_name, String mp4folder_path
        String filePath = mediaFile.getFilePath();
        String fileName = mediaFile.getFileName();
        String fileType = mediaFile.getFileType();
        String mp4_name = mediaFile.getFileId() + ".mp4";
        File originFile= new File(video_path+filePath+fileName);
        if(!originFile.exists()){
            return;
        }
//        如果avi则转为MP4
        mediaFile.setFileStatus("301001");
        if (fileType.equalsIgnoreCase("avi")) {

            Mp4VideoUtil mp4VideoUtil = new Mp4VideoUtil(ffmpeg_path, video_path + filePath + fileName, mp4_name, video_path + "mp4/");
            String mp4 = mp4VideoUtil.generateMp4();
            if ("success".equals(mp4)) {
                mediaFile.setProcessStatus("303002");
                //将MP4转为m3u8
                getM3u8(ffmpeg_path, video_path + "mp4/" + mp4_name, mediaFile.getFileId() + ".m3u8",  "hls/", mediaFileRepository, mediaFile);
            } else {
                mediaFile.setProcessStatus("303003");
                MediaFileProcess mediaFileProcess = new MediaFileProcess();
                mediaFileProcess.setErrormsg(mp4);
            }
        } else {
            mediaFile.setProcessStatus("303004");
        }
        mediaFileRepository.save(mediaFile);
//        返回结果
    }

    /**
     * @Description: 将MP4转为m3u8
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    private void getM3u8(
            String ffmpeg_path,
            String videoPath,
            String m3u8_name,
            String m3u8folder_path,
            MediaFileRepository mediaFileRepository,
            MediaFile mediaFile) {

        HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmpeg_path, videoPath, m3u8_name, video_path+m3u8folder_path);
        String m3u8 = hlsVideoUtil.generateM3u8();
        MediaFileProcess_m3u8 mediaFileProcess = new MediaFileProcess_m3u8();
        if ("success".equals(m3u8)) {
            List<String> ts_list = hlsVideoUtil.get_ts_list();
            mediaFileProcess.setTslist(ts_list);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess);
            mediaFile.setFileStatus("301002");
            mediaFile.setFileUrl(m3u8folder_path+m3u8_name);
        } else {
            mediaFile.setFileStatus("301003");
            mediaFileProcess.setErrormsg(m3u8);
        }
        mediaFileRepository.save(mediaFile);
    }
}
